home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / Cookie / Source / CookieFile.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.0 KB  |  208 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "CookieFile.h"
  5. #import <strings.h>
  6. #import <string.h>
  7. #import <c.h>
  8. #import <libc.h>
  9.  
  10. @implementation CookieFile
  11.  
  12. + (CookieFile *)findName :(char *)name fromList:(List *)list
  13. {
  14.     int count,maxcount;
  15.  
  16.     for(count = 0,maxcount = [list count]; count < maxcount; count++)
  17.     {
  18.         char *myfilename = [[list objectAt:count] filename];
  19.  
  20.         if(!strcasecmp(name,myfilename))
  21.             return [list objectAt:count];
  22.     }
  23.  
  24.     return NULL;
  25. }
  26.  
  27. - (int)filelength
  28. {
  29.     return file_length;
  30. }
  31.  
  32. - setFilelength :(int)length
  33. {
  34.     file_length = length;
  35.  
  36.     return self;
  37. }
  38.  
  39. - fillOut
  40. {
  41.     char mypath[MAXPATHLEN];
  42.     NXStream *stream;
  43.  
  44.     sprintf(mypath,"%s/Library/Cookies/%s",getenv("HOME"),filename);
  45.  
  46.     stream = NXMapFile(mypath, NX_READONLY);
  47.     if(!stream)
  48.     {
  49.         sprintf(mypath,"/LocalLibrary/Cookies/%s",filename);
  50.         stream = NXMapFile(mypath,NX_READONLY);
  51.     }
  52.  
  53.     if(!stream)
  54.         return 0;
  55.  
  56.     [self readCookies :stream];
  57.  
  58.     NXCloseMemory(stream, NX_FREEBUFFER);
  59.     return self;
  60. }
  61.  
  62. - initFromName :(char *)name fromList:(List *)list
  63. {
  64.     if([CookieFile findName :name fromList:list])
  65.         return FALSE;
  66.  
  67.     [self init];
  68.     strcpy(filename,name);
  69.  
  70.     return self;
  71. }
  72.  
  73. - init
  74. {
  75.     [super init];
  76.  
  77.     filename[0] = 0;
  78.     flags.selected = TRUE;
  79.     cookies = NULL;
  80.  
  81.     return self;
  82. }
  83.  
  84. - setSelected :(BOOL)value
  85. {
  86.     flags.selected = value;
  87.  
  88.     return self;
  89. }
  90.  
  91. - (BOOL)selected
  92. {
  93.     return flags.selected;
  94. }
  95.  
  96. - (BOOL)readCookies :(NXStream *)stream
  97. {
  98.     String *tempString;
  99.     BOOL foundCookie = FALSE;
  100.     Cookie *tempCookie;
  101.  
  102.     tempString = [[[String alloc] init] preAlloc:1024];
  103.     tempCookie = [[Cookie alloc] init];
  104.     if(!cookies)
  105.         cookies = [[List alloc] init];
  106.  
  107.     while([tempString readString:stream])
  108.     {
  109.         switch([tempString string][0])
  110.         {
  111.             case '#'    :
  112.                 if([tempString string][1] == '#')
  113.                     break;
  114.                 if(foundCookie)
  115.                 {
  116.                     if([tempCookie blank])
  117.                         [tempCookie free];
  118.                     else
  119.                         [cookies addObject:tempCookie];
  120.                     tempCookie = [[Cookie alloc] init];
  121.                     foundCookie = FALSE;
  122.                 }
  123.  
  124.                 [tempCookie addLine:&([tempString string][1])];
  125.                 foundCookie = TRUE;
  126.                 break;
  127.  
  128.             default    :
  129.                 if(foundCookie)
  130.                 {
  131.                     [tempCookie addLine:&([tempString string][1])];
  132.                 }
  133.             else
  134.                 {
  135.                     if([tempString string][0])
  136.                     {
  137.                         [tempCookie addLine:[tempString string]];
  138.                         [cookies addObject:tempCookie];
  139.                         tempCookie = [[Cookie alloc] init];
  140.                     }
  141.                 }
  142.             break;
  143.         }
  144.     }
  145.  
  146.  
  147.     if((foundCookie) && ![tempCookie blank])
  148.         [cookies addObject:tempCookie];
  149.     else
  150.         [tempCookie free];
  151.  
  152.     [tempString free];
  153.  
  154.     return TRUE;
  155. }
  156.  
  157. - (int)count
  158. {
  159.     if(!cookies)
  160.         [self fillOut];
  161.  
  162.     if(cookies)
  163.         return [cookies count];
  164.  
  165.     return 0;
  166. }
  167.  
  168. - (Cookie *)pickRandom
  169. {
  170.     if(!cookies)
  171.         [self fillOut];
  172.  
  173.     return [cookies objectAt:random()%[cookies count]];
  174. }
  175.  
  176. - (Cookie *)pickNumber :(int)number
  177. {
  178.     if(!cookies)
  179.         [self fillOut];
  180.  
  181.     return [cookies objectAt:number];
  182. }
  183.  
  184. - (void)free
  185. {
  186.     if(cookies)
  187.     {
  188.         [cookies freeObjects];
  189.         [cookies free];
  190.     }
  191.  
  192.     [super free];
  193. }
  194.  
  195. - (char *)filename
  196. {
  197.     return filename;
  198. }
  199.  
  200. - setFilename :(char *)string
  201. {
  202.     strcpy(filename,string);
  203.  
  204.     return self;
  205. }
  206.  
  207. @end
  208.